<route lang="json5" type="page">
{
	style: {
		navigationStyle: 'custom',
		navigationBarTitleText: '协议',
	},
}
</route>

<template>
	<view class="page-protocol">
		<fly-navbar
			:leftText="protocolType === 'agreement' ? '倍阅读用户协议' : '倍阅读隐私政策'"
			fixed
		/>
		<view v-if="thisPage === 1" class="w-full px-4 py-4 box-border">
			<rich-text :nodes="protocolRich"></rich-text>
		</view>
		<no-page v-if="thisPage === 0" :type="type" :title="title" @click="retry"></no-page>
	</view>
</template>

<script lang="ts" setup>
import { getProtocol } from '@/service/common';

const protocolType = ref('');
const protocolRich = ref('');
const title = ref('');
const thisPage = ref(-1);
const type = ref('msg');

onLoad(options => {
	if (options && options.type) {
		protocolType.value = options.type;
	} else {
		thisPage.value = 0;
		type.value = 'msg';
		title.value = '并未查询到协议,请返回重试';
	}
});

onShow(() => {
	if (protocolType.value) {
		getDetailsInfo();
	}
});

const getDetailsInfo = async () => {
	uni.showLoading({ title: '加载中...' });
	try {
		const res = await getProtocol(protocolType.value);
		uni.hideLoading();
		if (res.status_code === 200) {
			thisPage.value = 1;
			protocolRich.value = res.data;
			if (!res.data) {
				thisPage.value = 0;
				type.value = 'msg';
				title.value = '暂无协议信息';
			}
		} else {
			thisPage.value = 0;
			type.value = 'btn';
			title.value = res.message;
		}
	} catch (error) {
		uni.hideLoading();
		thisPage.value = 0;
		title.value = '服务器异常';
	}
};

const retry = () => {
	getDetailsInfo();
};
</script>

<style lang="scss" scoped></style>
